<?php
 class Lineorder extends CS_Controller {
 	
     private $orderState;
     private $orderStatus;
     public function _init()
     {
         $this->load->helper(array('common'));
         $this->load->model('user_model','user');
         $this->load->model('sales_order_model','sales_order');
         $this->load->model('line_order_product_model','line_order_product');
         $this->load->model('line_order_member_model','line_order_member');
         $this->load->model('line_order_refund_model','line_order_refund');
         $this->load->model('line_order_complain_model','line_order_complain');
         $this->load->model('line_product_reviews_model','line_product_reviews');
         $this->load->model('line_order_profit_model','line_order_profit');
         $this->load->model('line_price_model','line_price');
         $this->load->model('user_account_model','user_account');
         $this->load->model('account_log_model','account_log');
         $this->orderState = lineOrderStateArray();
         $this->orderStatus = lineOrderStatusArray();
     }
     
     public function see_line_menu()
     {
         $data['name'] = "旅游度假订单";
         $data['back_url'] = site_url('home/ucenter/index');
         $data['use_base_url'] = 'on';
         $data['line_order_status'] = $this->sales_order->findOrderByStatus($this->uid);
         $this->load->view('line/order/orderlinemenu',$data);
     }
     
     /**
      * 订单列表 & 订单列表搜索
      */
     public function order_reseller()
     {
         $pg = $this->input->get('page') ? $this->input->get('page') : '1';
         $page_num = 15;
         $num = ($pg-1)*$page_num;
         $order_status = $this->input->get('order_status');
         if (!$order_status || !is_numeric($order_status)){
              $_GET['order_status'] = 1;
              $order_status = 1;
         }
         $getData = $this->input->get();
         $getData['uid'] = $this->uid;
         $total = $this->sales_order->selectSalesOrderTotal($getData);
         $data['count'] = ceil($total/$page_num);
         $data['lineOrderStatusArray']= lineOrderStatusArray();
         $data['name'] = $data['lineOrderStatusArray'][$order_status];
         $data['back_url'] = "line/lineorder/see_line_menu";
         $data['saleOrder'] = $this->sales_order->selectSalesOrderList($page_num, $num, $getData);
         $data['order_status'] = $order_status;
         if( $data['saleOrder']->num_rows() > 0 ) {
             if( $pg == '1') {
                 $this->load->view('line/order/orderreseller',$data);
             }else{
                 echo json_encode(array('status'=>true,'content'=>$this->load->view('line/order/orderresellerlist',$data,true)));exit;
             }
         }else{
             echo json_encode(array('status'=>false,'content'=>'暂无内容'));exit;
         }
     }

     /**
      * 线路订单详情
      */
     public function line_order_detail()
     {
         $order_id = $this->input->get('order_id');
         $order_status = $this->input->get('order_status');
         $resultOrder = $this->sales_order->findByOrderId($order_id);
         if ($resultOrder->num_rows() <= 0) {
             $this->alertJumpPre('订单不存在。');
         }
         $data['salesorder'] = $resultOrder->row();
         $resultOrderProduct = $this->line_order_product->getProductByOrderId($order_id);
         $data['orderProductInfo'] = $resultOrderProduct->row();
         $data['lineOrderStateArray'] = $this->orderState;
         $data['lineOrderStatusArray'] = $this->orderStatus;
         $data['name'] = $data['lineOrderStatusArray'][$resultOrder->row()->order_status];
         $data['back_url'] = 'line/lineorder/order_reseller?order_status='.$order_status;
         $this->load->view('line/order/orderlinedetail',$data);
     }

     /**
      * 分销商取消订单操作。
      * @param $order_id
      */
     public function undoResellerOrder($order_id)
     {
         $this->db->trans_start();
         $resultOrder = $this->sales_order->findByOrderId($order_id);
         if ($resultOrder->num_rows() <= 0) {
             $this->error('line/lineorder/order_reseller',array('order_status'=>$this->input->get('order_status')), '取消订单失败！');
         }
         $saleOrder = $resultOrder->row();
         if ($saleOrder->order_status != 1 || $saleOrder->order_status != 2) { //验证订单是否已经付款
             $this->error('line/lineorder/order_reseller', array('order_status'=>$this->input->get('order_status')), '只有还未付款的才可以取消订单');
         }
         //退回库存
         $result = $this->line_order_product->findByOrderId($order_id);
         if ($result->num_rows() > 0) {
             $orderProduct = $result->row();
             $line_stock = $orderProduct->adult_num + $orderProduct->children_num;
             $this->line_price->returnLineStock($orderProduct, $line_stock);
         }
         //退回金币
         if ($saleOrder->pay_bonus > 0 && $saleOrder->uid&UTID_CUSTOMER) {
             $this->user_account->returnUserAccountUb($saleOrder->uid, $saleOrder->pay_bonus);
         }
         //取消订单
         $undoSuccess = $this->sales_order->updateOrderStatus($order_id,3);
         $this->db->trans_complete();
         if ($this->db->trans_status() === TRUE) {
             $this->success('line/lineorder/order_reseller', array('order_status'=>$this->input->get('order_status')), '取消订单成功！');
         } else {
             $this->error('line/lineorder/order_reseller', array('order_status'=>$this->input->get('order_status')), '取消订单失败！');
         }
     }
     
     /**
     * 申请退款页面。
     * @param unknown $order_id
     */
    public function refundResellerOrder($order_id)
    {
        $resultOrder = $this->sales_order->findByOrderId($order_id);
        if($resultOrder->num_rows() <= 0) {
            $this->alertJumpPre('订单不存在。');
        }
        $data['salesorder'] = $resultOrder->row();
        $resultOrderProduct = $this->line_order_product->getProductByOrderId($order_id);
        $data['orderProductInfo'] = $resultOrderProduct->row(0);
        $data['name'] = '申请退款';
        $data['back_url'] = 'line/lineorder/line_order_detail?order_id='.$order_id.'&order_status='.$this->input->get('order_status');
        $this->load->view('line/order/refundResellerOrder',$data);
    }

     /**
      * 申请退款操作。
      */
    public function refundResellerOrderPost()
    {
        $order_id = trim($this->input->post('order_id'));
        $refundOrder = $this->line_order_refund->getRefundDetailByOrderId($order_id);
        if( $refundOrder->num_rows() > 0 ){
            $this->alertJumpPre('该订单已申请退款');
        }
        $this->db->trans_start();
        $refundSuccess = $this->line_order_refund->insertLineOrderRefund($this->input->post()); //申请退款
        $this->db->trans_complete();
        if ($refundSuccess) {
            $this->success('line/lineorder/order_reseller', array('order_status'=>$this->input->post('order_status')), '您已申请退款！！');
        } else {
            $this->error('line/lineorder/refundResellerOrder/'.$this->input->post('order_id'), array('order_status'=>$this->input->post('order_status')), '申请退款失败！');
        }
     }
     
     /**
      * 订单产品评价详情
      * @param unknown $order_id
      */
     public function reviewProduct($order_id)
     {
         $resultOrder = $this->sales_order->findByOrderId($order_id);
         if($resultOrder->num_rows() <= 0) {
             $this->alertJumpPre('订单不存在。');
         }
         $data['salesorder'] = $resultOrder->row();
         $resultOrderProduct = $this->line_order_product->getProductByOrderId($order_id);
         $data['orderProductInfo'] = $resultOrderProduct->row(0);
         $data['name'] = '评价';
         $data['back_url'] = 'line/lineorder/line_order_detail?order_id='.$order_id.'&order_status='.$this->input->get('order_status');
         $this->load->view('line/order/reviewProduct',$data);
     }


     /**
      * 订单产品评价保存
      */
     public function saveProductReview()
     {
         $error = array();
         if ($this->validateParam($this->input->post('user_content'))) {
             $error[] = '请填写评分内容。';
         }
         if (!empty($error)) {
             $this->alertJumpPre(implode('\\n', $error));
         }
         $getData = $this->input->post();
         $getData['uid'] = $this->uid;
         $getData['user_name'] = $this->userName;
         $this->db->trans_start();
         $resultId = $this->line_product_reviews->insertLineProductReviews($this->input->post());
         //确认完成，账户需分钱操作。
         $orderProfit = $this->line_order_profit->findByOrderId($this->input->post('order_id'));
         if ($orderProfit->num_rows() > 0) {
             $isOk = $this->user_account->userAccountProfit($orderProfit->result());
         }
         //修改订单状态
         $this->sales_order->modifyOrderStatus($this->input->post('order_id'), 3, 5); //状态为3和5
         $this->db->trans_complete();
         if ($this->db->trans_status() === TRUE) {
             $this->success('line/lineorder/order_reseller', $this->input->get('order_status'), '评价成功！');
         } else {
             $this->error('line/lineorder/order_reseller', $this->input->get('order_status'), '评价失败！');
         }
     }
     
    /**
     * 查看评价
     * @param unknown $order_id
     */
    public function showReview($order_id)
    {
        $resultOrder = $this->sales_order->findByOrderId($order_id);
        if($resultOrder->num_rows() <= 0) {
            $this->alertJumpPre('订单不存在。');
        }
        $data['salesorder'] = $resultOrder->row();
        $resultOrderReview = $this->line_order_product->getReviewByOrderId($order_id);
        $data['orderReviews'] = $resultOrderReview->row(0);
        $data['name'] = '查看评价';
        $data['back_url'] = 'line/lineorder/line_order_detail?order_id='.$order_id.'&order_status='.$this->input->get('order_status');
        $this->load->view('line/order/showReview', $data);
    }
    
    
    /**
     * 投诉页面
     * @param unknown $order_id
     */
    public function resellerComplain($order_id)
    {
        $resultOrder = $this->sales_order->findByOrderId($order_id);
        if($resultOrder->num_rows() <= 0) {
            $this->alertJumpPre('订单不存在。');
        }
        $data['salesorder'] = $resultOrder->row();
        $resultOrderProduct = $this->line_order_product->getProductByOrderId($order_id);
        $data['orderProductInfo'] = $resultOrderProduct->row(0);
        $data['name'] = '投诉';
        $data['back_url'] = 'line/lineorder/line_order_detail?order_id='.$order_id.'&order_status='.$this->input->get('order_status');
        $this->load->view('line/order/resellerComplain', $data);
    }
    
    
    /**
     * 提交投诉申请
     */
    public function resellerApplyComplainPost()
    {
        $this->db->trans_start();
        $result = $this->line_order_complain->insertLineOrderComplain($this->input->post());
        $this->db->trans_complete();
        if ($this->db->trans_status() === TRUE) {
            $this->success('line/lineorder/order_reseller', array('order_status'=>$this->input->get('order_status')), '投诉成功！');
        } else {
            $this->error('line/lineorder/resellerComplain'.$this->input->get('order_id'), array('order_status'=>$this->input->get('order_status')), '投诉失败！');
        }
    }
}